//
//  AppTools.m
//  UI_Custom
//
//  Created by yutao on 15/9/9.
//  Copyright (c) 2015年 yutao. All rights reserved.
//

#import "AppTools.h"

@implementation AppTools
+(void)GetURLWithString:(NSString *)urlStr SuccessBlock:(AppToolsBlockSuccess)successBlock andFailureBlock:(AppToolsBlockFailure)failureBlock
{
    NSString *urlEncod = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlEncod];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSError *error = nil;


        
     id responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

        if (responseObject != nil) {
            successBlock(responseObject);
        }else
        {
            failureBlock(error);
        }


    }];

}

+(void)DownLoadImageWithURL:(NSString *)urlStr Block:(DownImageBlock)block
{
    NSString *urlEncod = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:urlEncod];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        block(data);
        
    }];
    
    

}

+(void)getAFNetWorkingData:(NSString *)url Block:(AFNetBlock)block FailureBlock:(AFNetFailure)failureBlock
{
    NSString *url_string = [NSString stringWithFormat:@"%@",url];
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
               block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        failureBlock(error);
        //NSLog(@"失败==== %@",error);
        
    }];
   
}



@end






